package com.learn.english.controller;

import com.learn.english.domain.R;
import com.learn.english.model.entity.KnowledgeBase;
import com.learn.english.service.KnowledgeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 知识库管理控制器
 */
@RestController
@RequestMapping("/knowledge")
public class KnowledgeBaseController {

    @Resource
    private KnowledgeService knowledgeService;

    /**
     * 获取知识条目列表（支持分页）
     * @param params 查询参数
     * @return 知识条目列表或分页数据
     */
    @GetMapping("/list")
    public R<Map<String, Object>> getKnowledgeList(@RequestParam Map<String, Object> params) {
        // 判断是否需要分页
        if (params.containsKey("pageNum") && params.containsKey("pageSize")) {
            return R.ok(knowledgeService.getKnowledgeListByPage(params));
        } else {
            List<KnowledgeBase> list = knowledgeService.getKnowledgeList(params);
            Map<String, Object> result = new HashMap<>();
            result.put("list", list);
            result.put("total", list.size());
            return R.ok(result);
        }
    }

    /**
     * 获取单个知识条目详情
     * @param id 知识条目ID
     * @return 知识条目对象
     */
    @GetMapping("/{id}")
    public R<KnowledgeBase> getKnowledgeDetail(@PathVariable Long id) {
        return R.ok(knowledgeService.getKnowledgeById(id));
    }

    /**
     * 添加知识条目
     * @param knowledgeBase 知识条目对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public R<Map<String, Object>> addKnowledge(@RequestBody KnowledgeBase knowledgeBase) {
        boolean success = knowledgeService.addKnowledge(knowledgeBase);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "添加成功" : "添加失败");
        return R.ok(result);
    }

    /**
     * 更新知识条目
     * @param knowledgeBase 知识条目对象
     * @return 操作结果
     */
    @PostMapping("/update")
    public R<Map<String, Object>> updateKnowledge(@RequestBody KnowledgeBase knowledgeBase) {
        boolean success = knowledgeService.updateKnowledge(knowledgeBase);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "更新成功" : "更新失败");
        return R.ok(result);
    }

    /**
     * 删除知识条目
     * @param id 知识条目ID
     * @return 操作结果
     */
    @GetMapping("/delete/{id}")
    public R<Map<String, Object>> deleteKnowledge(@PathVariable Long id) {
        boolean success = knowledgeService.deleteKnowledge(id);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "删除成功" : "删除失败");
        return R.ok(result);
    }

    /**
     * 批量删除知识条目
     * @param ids 知识条目ID数组
     * @return 操作结果
     */
    @PostMapping("/batch")
    public R<Map<String, Object>> batchDeleteKnowledge(@RequestBody List<Long> ids) {
        boolean success = knowledgeService.batchDeleteKnowledge(ids);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "批量删除成功" : "批量删除失败");
        return R.ok(result);
    }
}
